<?php
include 'simple_html_parser/simple_html_dom.php';

function getStartingChild ($root) {
    return $root->childNodes(0)
                ->children(1)
                ->children(0)
                ->children(2)
                ->children(0)
                ->children(0)
                ->children(3);
}

function decode ($str) {
    return urldecode($str);
}

function encode ($str) {
    return urlencode($str);
}

function addToFile ($URI, $word) {
    $dom = new DomDocument();
    $dom->preserveWhiteSpace = false;
    $dom->load($URI);
    $dom->formatOutput = true;
    $listeMots = $dom->getElementsByTagName("word");
    $write = true;
    
    foreach($listeMots as $mot) {
        if ($mot->getAttribute("lemme") == $word['lemme']) {
            $write = false;
            break;
        }
    }

    if ($write) {
        $newWord = $dom->createElement('word');
        $newWord->setAttribute('lemme', $word['lemme']);

        foreach($word['synonyms'] as $synonym) {
            $newSynonym = $dom->createElement('synonym');
            $newSynonym->setAttribute('lemme', $synonym['lemme']);
            $newSynonym->setAttribute('context', $synonym['context']);
            
            $boolean;
            if ($synonym['opposit']) {
                $boolean = "true";
            }
            else {
                $boolean = "false";
            }
            $newSynonym->setAttribute('opposit', $boolean);

            $newWord->appendChild($newSynonym);
        }

        $dom->importNode($newWord, true);
        $dom->documentElement->appendChild($newWord);

        $dom->save($URI);

        echo "Le mot '" . utf8_decode($word['lemme']) . "' a ete ajoute au dictionnaire.<br/>\n";
    }
    else {
        echo "Le mot '" . utf8_decode($word['lemme']) . "' est deja dans le dictionnaire.<br/>\n";
    }
}

function createWordArray ($word) {
    $array['lemme'] = utf8_encode($word);
    $array['synonyms'] = array();

    return $array;
}

function addSynonym ($array, $word, $context, $opposit) {
    $synonym['lemme'] = utf8_encode($word);
    $synonym['context'] = utf8_encode($context);
    $synonym['opposit'] = $opposit;

    $array['synonyms'][] = $synonym;
    return $array;
}

function getLemme ($str) {
    $pos = strpos($str, ",");
    return substr($str, 0, $pos);
}

function getContext($str) {
    return substr($str, 1, strlen($str)-2);
}

function addWord ($file, $mot) {
    $mot = encode($mot);
    $website = "http://www.synonymes.com/";
    $URL = $website . "synonyme.php?mot=" . $mot;

    $root = file_get_html($URL);

    if (!verifieAdress($root)) {
        echo "Ce mot n'existe pas.<br/>\n";
        return;
    }

    $start = getStartingChild($root);

    for ($element = $start; $element != null; $element = $element->nextSibling()) {
        $word = $element->childNodes(1)->childNodes(1)->plaintext;
        $word = decode($word);

        $word = getLemme($word);
        $word = createWordArray($word);

        $element1 = $element->childNodes(2)
                            ->childNodes(1)
                            ->childNodes(0)
                            ->childNodes(0)
                            ->childNodes(3);

        foreach ($element1->find('table') as $element2) {

            $context = "general";
            $opposit = false;

            foreach($element2->find('span') as $element3) {
                if ($element3->class != null) {
                    if ($element3->class == 'arial-12-vert-b2') {
                        $opposit = true;
                    }
                    else {
                        $context = $element3->plaintext;
                        $context = getContext($context);
                        $context = decode($context);
                    }
                }
                else {
                    $element3 = decode($element3->plaintext);

                    $word = addSynonym ($word,$element3,$context,$opposit);
                }
            }
        }
        addToFile($file,$word);
    }
}

function verifieAdress($root) {
    $tmp = getStartingChild($root);
    if ($tmp->plaintext != "")
        return true;

    return false;
}

function addWordsRecursive ($file, $word, $limit) {
    $words[] = encode($word);
    $website = "http://www.synonymes.com/";

    for ($compteur = 0; $compteur < $limit and !empty($words); $compteur ++) {
        $words = array_values($words);
        $mot = $words[0];
        unset ($words[0]);

        $URL = $website . "synonyme.php?mot=" . $mot;

        $root = file_get_html($URL);

        if (!verifieAdress($root)) {
            echo "Ce mot n'existe pas.<br/>\n";
        }
        else {
            $start = getStartingChild($root);

            for ($element = $start; $element != null; $element = $element->nextSibling()) {
                $word = $element->childNodes(1)->childNodes(1)->plaintext;
                $word = decode($word);

                $word = getLemme($word);
                $word = createWordArray($word);

                $element1 = $element->childNodes(2)
                                    ->childNodes(1)
                                    ->childNodes(0)
                                    ->childNodes(0)
                                    ->childNodes(3);

                foreach ($element1->find('table') as $element2) {

                    $context = "general";
                    $opposit = false;

                    foreach($element2->find('span') as $element3) {
                        if ($element3->class != null) {
                            if ($element3->class == 'arial-12-vert-b2') {
                                $opposit = true;
                            }
                            else {
                                $context = $element3->plaintext;
                                $context = getContext($context);
                                $context = decode($context);
                            }
                        }
                        else {
                            $element3 = decode($element3->plaintext);

                            $words[] = $element3;
                            $word = addSynonym ($word,$element3,$context,$opposit);
                        }
                    }
                }
                addToFile($file,$word);
            }
        }
    }

    if($compteur < $limit) {
        echo utf8_decode("<br/>\nTous les mots ont été ajoutés.<br/>\n");
    }
    else {
        echo utf8_decode("<br/>\nLa limite de mot à ajouter a été atteinte.<br/>\n");
        echo utf8_decode("<br/>\nIl reste encore " . count($words) . " mot dans la liste<br/>\n");
    }
}

?>